package edu.pku.sei.act.classGenerator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import edu.pku.sei.act.taskStruct.*;
import edu.pku.sei.act.xmlTools.XmlProcessor;

public class ClassTester {

	/**
	 * @param args
	 */
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		String base_dir = null;
		String path = null;
		try {
			base_dir = new File("").getCanonicalPath();
			path = base_dir + "\\task.xml";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println(path);		
		Document document = XmlProcessor.readDocument(new File(path));
		Task task = XmlProcessor.genTaskStruct(document);		
						
		String package_base_dir = base_dir + "\\output\\src" + "\\edu\\pku\\sei\\act\\" + task.getName();
		File package_base = new File(package_base_dir);
		package_base.mkdirs();
						
		try {
			File file = new File(package_base, NameGenerator.genTaskProcessorName(task.getName()) + ".java");
			file.createNewFile();
			FileOutputStream fostream = new FileOutputStream(file);
			fostream.write(new TaskProcessorClassGenerator().generate(task).getBytes());
			
			file = new File(package_base, NameGenerator.genTaskUIName(task.getName()) + ".java");
			file.createNewFile();
			fostream = new FileOutputStream(file);
			fostream.write(new TaskUIClassGenerator().generate(task).getBytes());
			
			file = new File(package_base, NameGenerator.genInitTransitionName(task.getInitTrans().getName()) + ".java");
			file.createNewFile();
			fostream = new FileOutputStream(file);
			fostream.write(new InitTransitionClassGenerator().generate(task).getBytes());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
													
		Collection roles = task.getRoles();		
		for(Iterator<Role> it = roles.iterator(); it.hasNext(); )
		{			
			try {
				Role role = it.next();
				File role_package_base = new File(package_base, NameGenerator.genRolePackageName(role.getName()));
				role_package_base.mkdirs();
				File file = new File(role_package_base, NameGenerator.genRoleName(role.getName())+".java");
				file.createNewFile();
				FileOutputStream fostream = new FileOutputStream(file);
				fostream.write(new RoleClassGenerator().generate(task.getName(), role).getBytes());
								
				Collection<Transition> transes = role.getTranses();
				for(Iterator<Transition> it2 = transes.iterator(); it2.hasNext(); )
				{
					Transition trans = it2.next();									
					file = new File(role_package_base, NameGenerator.genTransitionName(trans.getName())+".java");
					file.createNewFile();
					fostream = new FileOutputStream(file);					
					fostream.write(new TransitionClassGenerator().generate(task.getName(), role, trans).getBytes());
				}				
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}						
	}

}
